
#include <cstdio>
#include <BOpenGL>
#include <BPainter>
#include <BImage>
#include <BCamera>

#include <BElevation>
#include "member_BElevation.h"

using namespace BWE;

#define member					(*(member_BElevation*)_ptr)
#define member_allocate()		_ptr = new member_BElevation(this)
#define member_release()		delete (member_BElevation*)_ptr

BElevation::BElevation()
{
	member_allocate();
}
BElevation::BElevation(const BSize& size, Format format)
{
	member_allocate();
	this->setSize(size);
	this->setFormat(format);
}
BElevation::~BElevation()
{
	member_release();
}

void BElevation::setFormat(Format format)
{
	if (member.format != format)
	{
		member.format = format;
		member.formatLen = 0;

		if (format == Format_Char)
			member.formatLen = sizeof(char);
		if (format == Format_Short)
			member.formatLen = sizeof(short);
		if (format == Format_Float)
			member.formatLen = sizeof(float);
	}
}
Format BElevation::format() const
{
	return member.format;
}

void BElevation::setSize(const BSize& size)
{
	if (member.size != size)
	{
		member.size = size;
		member.data = 0;
	}
}
void BElevation::setSize(int width, int height)
{
	setSize(BSize(width, height));
}
const BSize& BElevation::size() const
{
	return member.size;
}

void BElevation::setWidth(int width)
{
	BSize size(width, member.size.height());
	setSize(size);
}
int BElevation::width() const
{
	return member.size.width();
}

void BElevation::setHeight(int height)
{
	BSize size(member.size.width(), height);
	setSize(size);
}
int BElevation::height() const
{
	return member.size.height();
}

void BElevation::setData(BByteArray* data)
{
	member.data = data;
}
BByteArray* BElevation::data()
{
	return member.data;
}
const BByteArray* BElevation::data() const
{
	return member.data;
}

void BElevation::setValue(int x, int y, float value)
{
	if (member.realize())
	{
		int index = member.formatLen * y + x;
		if (member.format == Format_Float)
		{
			float* ptr = (float*)(member.data->data() + index);
			*ptr = value;
		}
		else if (member.format == Format_Short)
		{
			short* ptr = (short*)(member.data->data() + index);
			*ptr = (short)value;
		}
		if (member.format == Format_Char)
		{
			char* ptr = (char*)(member.data->data() + index);
			*ptr = (char)value;
		}
		member.dirty = true;
	}
}
float BElevation::value(int x, int y) const
{
	if (member.realize())
	{
		int index = member.formatLen * y + x;
		if (member.format == Format_Float)
		{
			float* ptr = (float*)(member.data->data() + index);
			return *ptr;
		}
		else if (member.format == Format_Short)
		{
			short* ptr = (short*)(member.data->data() + index);
			return *ptr;
		}
		else if (member.format == Format_Char)
		{
			char* ptr = (char*)(member.data->data() + index);
			return *ptr;
		}
	}
	return 0;
}

void BElevation::clear()
{
	member.size = 0;
	member.data = 0;
	BMesh::clear();
}

bool BElevation::empty() const
{
	return member.formatLen == 0 || member.size == BSize();
}

bool BElevation::fresh()
{
	return true;
}




